var parentData, parentActive;
var dateFormatStr = 'yyyy-MM-dd HH:mm:ss';
layui.use(['form', 'table', 'layer', 'laydate'], function () {
	var form = layui.form,
		layer = layui.layer,
		table = layui.table,
		$ = layui.$;

	var imgArtwork, imgUrl;
	window.active = {};
	//操作
	$('.layui-btn').on('click', function () {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	//表格头部按钮操作
	table.on('toolbar(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data) : '';
	});
	//表格按钮操作
	table.on('tool(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data) : '';
	});

	async function getPrice(){
		let res=await ajaxAsyncNoTip('banner/prize',{},'GET')
			//初始化数据
			form.val("dataform", res);
			$('.prize').val(res.prize)
			$('.iscore').val(res.iscore)
			$('.iticket').val(res.iticket)
			$('.uscore').val(res.uscore)
			$('.uticket').val(res.uticket)
			if (res.img) {
				imgUrl = res.img;
				$(".pic-cont").css('background-size', 'cover');
				$(".pic-cont").css('background-image', `url(${res.img})`);
			}
	}
	getPrice()
	
	form.verify({});
	form.on("submit(save)", element => {
		
		if (js.isNullOrUndefined(imgArtwork) && js.isNullOrUndefined(imgUrl)) {
			top.layer.msg('请上传头像', {
				icon: 2,
				time: 1000 //2秒关闭（如果不配置，默认是3秒）
			});
		}
		(async function () {
			let data = form.val("dataform");
			console.log(data);
			if (!js.isNullOrUndefined(imgArtwork) && imgUrl != imgArtwork.path) {
				const formData = new FormData();
				var nameImg = new Date().getTime() + '.jpg';
				formData.append('file', imgArtwork, nameImg);
				let res = await ajaxAsyncNoTip('upload', formData, 'POST', () => { }, { // 请求后台接口
					processData: false,
					contentType: false
				});
				if (res.upload_files.length) {
					let resImg = res.upload_files[0];
					if (resImg.code != 0) {
						top.layer.alert(resImg.msg + ":" + resImg.info);
						return;
					}
					else {
						imgUrl = resImg.data.imgPath;
					}
				} else {
					top.layer.alert('上传头像失败');
					return;
				}
			}
			 img = imgUrl;
			 prize=data.prize;
			 iscore=data.iscore;
			 iticket=data.iticket;
			 uscore=data.uscore;
			 uticket=data.uticket;
			await ajaxAsync('banner/upd', {img,prize,iscore,iticket,uscore,uticket}, 'POST');
		})();
		return false;
	});

	$('.pic-cont').click(function () {
		document.getElementById('J-update-img-file').click();
	});
	$('#J-update-img-file').on('change', async function (e) {
		if (!this.files[0] || !window.FileReader) return;
		if (this.files[0].type !== 'image/jpeg') {
			alert('请选择jpg格式的图片!');
			return;
		}

		imgArtwork = await ci.compressImg(this.files[0], {
			size: 0.5,
			Rectangle: true
		});
		imgArtwork.path = URL.createObjectURL(imgArtwork);
		imgArtwork.name = this.files[0].name;
		// $(".J-user-header-img").attr('src', imgUrl);
		$(".pic-cont").css('background-size', 'cover');
		$(".pic-cont").css('background-image', `url(${imgArtwork.path})`);
		return;
	});
});